<?php

namespace App\Http\Controllers\Admin\Material;

use App\Api\Exceptions\ApiCommonException;
use App\Http\Controllers\Admin\AdminController;
use Illuminate\Http\Request;
use App\Models\Material;
use App\Handlers\Oss;
use Storage;
use Validator;
use Lang;

class MaterialsController extends AdminController
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Material $material)
    {
        $file = $request->file('media');
        $media_type = $request->input('media_type', 1);

        // 创建验证规则
        $rules = [
            'media'      => 'required',
        ];

        // 自定义验证消息
        $messages = [
            'media.required'      => Lang::get('material.media').Lang::get('common.required'),
        ];

        //开始验证
        $validator = Validator::make($request->all(), $rules, $messages);

        if($validator->fails())
        {
            throw ApiCommonException::requestParamsErr($validator->messages()->first());
        }

        $file_extension = strtolower($file->getClientOriginalExtension());
        $options = [];

        switch ($media_type) {
            case 1:
                $extension_allow = config('filesystems.image_extension_allow');
                $max_file_size = config('filesystems.image_max_size');
                $options = ['ContentType'=>'image/'.$file_extension];
                break;

            case 4:
                $extension_allow = config('filesystems.file_extension_allow');
                $max_file_size = config('filesystems.file_max_size');
                break;

            default:
                $extension_allow = config('filesystems.image_extension_allow');
                $max_file_size = config('filesystems.image_max_size');
        }

        if (!in_array($file_extension, $extension_allow)) {
            throw ApiCommonException::requestParamsErr(Lang::get('material.media').Lang::get('common.not_extension'));
        }

        $file_size = $file->getClientSize();

        if ($file_size > $max_file_size) {
            throw ApiCommonException::requestParamsErr(Lang::get('common.file_max_size'));
        }

        $file = $file->storeAs('meeting', md5(uniqid(time())).'.'.$file_extension);

        $exists = Storage::exists($file);

        if (!$exists) {
            throw ApiCommonException::requestParamsErr(Lang::get('common.file_upload_exception'));
        }

        $real_path = storage_path('app/'.$file);

        $media_id = $file;

        //阿里 OSS 图片上传
        try {
            OSS::publicUpload($media_id, $real_path, $options);
        } catch (\Exception $e) {
            throw ApiCommonException::requestParamsErr(Lang::get('common.oss_exception'));
        }

        $data['media_type'] = $media_type;
        $data['bucket_name'] = config('alioss.BucketName');
        $data['media_id'] = $media_id;

        $r = $material->create($data);

        Storage::delete($file);

        $result['id'] = $r->id;
        $result['media_type'] = $media_type;
        $result['media_id'] = $media_id;
        $result['media_real_url'] = Oss::getPublicObjectURL($media_id);

        return $this->getResponseSuccess($result);
    }
}
